package com.gillion.grape.fros.ui.util;

/**
 * 主要提供增量编辑的替换功能
 * @Author   Alex
 * @Version  1.0
 * @Date     2010-10-26
 * @example  使用示例：略。
 */
public class ReplaceUtil {

	// 定义不同的替换类型
	public static final int PATTERN_TYPE_XML = 0;
	public static final int PATTERN_TYPE_JAVA = 1;
	public static final int PATTERN_TYPE_PROPERTIES = 2;
	public static final int PATTERN_TYPE_NO_REPLACE = -1;
	
	/**
	 * 判断改类型是否受增量编辑支持
	 * @param ReplaceUtil.PATTERN_TYPE_JAVA etc
	 * @return
	 */
	public static boolean isSupportType(int type) {
		if(type == PATTERN_TYPE_XML) {
			return true;
		}
		if(type == PATTERN_TYPE_JAVA) {
			return true;
		}
		if(type == PATTERN_TYPE_PROPERTIES) {
			return true;
		}
		return false;
	}
	
	// 获取标志块的开头
	private static String getStartPatten(int pageType) {
		if(pageType == PATTERN_TYPE_XML) {
			return XMLPattern.START;
		} else if(pageType == PATTERN_TYPE_JAVA) {
			return JavaPattern.START;
		} else if(pageType == PATTERN_TYPE_PROPERTIES) {
			return PropertiesPattern.START;
		}
		return "";
	}
	
	// 获取标志块的结束
	private static String getEndPatten(int pageType) {
		if(pageType == PATTERN_TYPE_XML) {
			return XMLPattern.END;
		} else if(pageType == PATTERN_TYPE_JAVA) {
			return JavaPattern.END;
		} else if(pageType == PATTERN_TYPE_PROPERTIES) {
			return PropertiesPattern.END;
		}
		return "";
	}
	
	// 定义不同文件的标志块
	private static class XMLPattern {
		public static final String START = "<!-- Put custom code below -->";
		public static final String END="<!-- Put custom code above -->";
	}
	
	private static class JavaPattern {
		public static final String START = "/** Put custom code below **/";
		public static final String END="/** Put custom code above **/";
	}
	
	private static class PropertiesPattern {
		public static final String START = "# Put custom code below";
		public static final String END="# Put custom code above";
	}

	/**
	 * 将目标文件自定义部分替换到JET生成文件中
	 * @param JET生成的源文件内容
	 * @param 目标文件内容
	 * @param 指定文件类型（XML JAVA等）
	 * @return 替换后的字符串
	 */
	public static String replace(String source, String target, int pattern) {
		if(source == null || target == null)
			return null;
		int sourceStartPos = 0;
		int sourceEndPos = 0;
		int targetStartPos = 0;
		int targetEndPos = 0;
		String strToInsert = new String();
		String strSrcToInsert = new String();
		while(true) {
			sourceStartPos = source.indexOf(getStartPatten(pattern), sourceEndPos + strToInsert.length() - strSrcToInsert.length() + getEndPatten(pattern).length());
			sourceEndPos = source.indexOf(getEndPatten(pattern), sourceEndPos + strToInsert.length() - strSrcToInsert.length() + getEndPatten(pattern).length());
			targetStartPos = target.indexOf(getStartPatten(pattern), targetEndPos + getEndPatten(pattern).length());
			targetEndPos = target.indexOf(getEndPatten(pattern), targetEndPos + getEndPatten(pattern).length());
			if(sourceStartPos == -1
					|| sourceEndPos == -1
					|| targetStartPos == -1
					|| targetEndPos == -1) {
				break;
			}
			strToInsert = target.substring(targetStartPos, targetEndPos);
			strSrcToInsert = source.substring(sourceStartPos, sourceEndPos);
			source = new StringBuilder(source.substring(0, sourceStartPos))
						.append(strToInsert)
						.append(source.substring(sourceEndPos))
						.toString();
		}
		return source;
	}
	
}